namespace ScrumSprintMonitor.TestbenchApp
{
	#region #using Directives

	using System;
	using System.Diagnostics;
	using System.Globalization;
	using System.Windows;

	using Extensibility;

	#endregion

	public class ResourceManager : IResourceManager
	{
		#region Fields

		private readonly ResourceDictionary mainDictionary;

		#endregion

		#region Properties

		#endregion

		#region Construction

		public ResourceManager()
		{
			Debug.Assert(Application.Current != null, "System.Windows.Application.Current != null");
			this.mainDictionary = Application.Current.Resources;
		}

		#endregion

		#region IResourceManager Members

		public void MergeDictionary(string resourceDictionaryUri)
		{
			if (resourceDictionaryUri == null)
			{
				throw new ArgumentNullException("resourceDictionaryUri");
			}

			var dictionary = new ResourceDictionary {
			                                        	Source = new Uri(resourceDictionaryUri)
			                                        };

			MergeDictionary(dictionary);
		}

		public object GetResource(string resourceKey)
		{
			if (string.IsNullOrEmpty(resourceKey))
			{
				Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "The resource {0} is not registered on the mainDictionary.", resourceKey));
				return null;
				// TODO: Uncomment this when we have all the resources in place...
				// throw new ArgumentNullException("resourceKey");
			}

			Debug.Assert(this.mainDictionary != null, "mainDictionary != null");

			if (!this.mainDictionary.Contains(resourceKey))
			{
				Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "The resource {0} is not registered on the mainDictionary.", resourceKey));
				// TODO: Uncomment this when we have all the resources in place...
				//    throw new ArgumentException(
				//        string.Format("The resource {0} is not registered on the mainDictionary.", resourceKey),
				//        "resourceKey");
			}

			return this.mainDictionary[resourceKey];
		}

		#endregion

		public void MergeDictionary(ResourceDictionary resourceDictionary)
		{
			if (resourceDictionary == null)
			{
				throw new ArgumentNullException("resourceDictionary");
			}

			Debug.Assert(this.mainDictionary != null, "mainDictionary != null");

			this.mainDictionary.MergedDictionaries.Add(resourceDictionary);
		}
	}
}